package leetcode;

/**
 * @author:xufengxiang
 * @date 2023-03-14 20:01
 */
public class Code13 {

    public static int romanToInt(String s) {
        int[] values = {1000, 900,500,400,100,90,50,40,10,9,5,4,1};
        String[] characters = {"M", "CM", "D", "CD", "C","XC", "L","XL","X", "IX","V","IV", "I"};

        int num = 0;
        int length = s.length();

        int index = 0;

        int maxIndex = characters.length;
        for(int i=0;i<length && index < maxIndex;){

            String substring1 = s.substring(i, i + 1);
            if(substring1.equals(characters[index])){
                num +=values[index];
                i++;
                continue;
            }

            if(i < length-1) {
                String substring2 = s.substring(i, i + 2);
                if (substring2.equals(characters[index])) {
                    num += values[index];
                    i = i + 2;
                    continue;
                }
            }
            index++;
        }
        return num;
    }

    public static void main(String[] args) {
        int roman = romanToInt("DCXXI");
        System.out.println(roman);
    }
}
